package com.mirama.dashboard.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping
@Slf4j
public class HelloController {

    @GetMapping({"", "/hello"})
    public Map<String, Object> hello(@RequestParam(defaultValue = "dashboard") String key) {
        String datetime = DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MS_FORMAT);
        log.info(datetime);
        Map<String, Object> result = new HashMap<>();
        result.put("datetime", datetime);
        result.put("msg", String.format("hello %s", key));
        return result;
    }

    /**
     * 测试session共享 登录，操作真简单。
     * 1、maven添加 spring-session-data-redis
     * 2、application.yaml配置redis
     * <p>
     * dashbord_8004
     * 2019-12-02 16:31:06.546  INFO 6 --- [nio-8080-exec-9] c.m.d.controller.HelloController         : 测试session共享，{"msg":"首次访问","sessionId":"fb85cb76-bb2f-414c-95bd-3128728477fa"}
     * 2019-12-02 16:31:06.934  INFO 6 --- [io-8080-exec-10] c.m.d.controller.HelloController         : 测试session共享，{"msg":"欢迎光临","sessionId":"fb85cb76-bb2f-414c-95bd-3128728477fa","username":"47f68e4f"}
     * 2019-12-02 16:31:07.375  INFO 6 --- [nio-8080-exec-1] c.m.d.controller.HelloController         : 测试session共享，{"msg":"欢迎光临","sessionId":"fb85cb76-bb2f-414c-95bd-3128728477fa","username":"47f68e4f"}
     * dashbord_8003
     * 2019-12-02 16:31:06.750  INFO 6 --- [nio-8080-exec-2] c.m.d.controller.HelloController         : 测试session共享，{"msg":"欢迎光临","sessionId":"fb85cb76-bb2f-414c-95bd-3128728477fa","username":"47f68e4f"}
     * 2019-12-02 16:31:07.138  INFO 6 --- [nio-8080-exec-3] c.m.d.controller.HelloController         : 测试session共享，{"msg":"欢迎光临","sessionId":"fb85cb76-bb2f-414c-95bd-3128728477fa","username":"47f68e4f"}
     *
     * @param session session
     * @return sessionId
     */
    @GetMapping("/index")
    public Map<String, Object> login(HttpSession session) {

        Map<String, Object> result = new HashMap<>();

        // 获取sessionid
        String sessionId = session.getId();
        result.put("sessionId", sessionId);

        // 从session中获取用户名
        Object username = session.getAttribute("username");
        if (ObjectUtils.isEmpty(username)) {
            // 用户名为空，首次访问，放入session
            username = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
            session.setAttribute("username", username);

            result.put("msg", "首次访问");
        } else {
            String usernameStr = ObjectUtils.nullSafeToString(username);
            result.put("sessionId", sessionId);
            result.put("username", username);
            result.put("msg", "欢迎光临");
        }

        log.info("测试session共享，{}", JSON.toJSONString(result));

        return result;
    }

}
